// pages/companyIndex/companyIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 下拉列表
     */
    years: ["选择年份", "2018", "2019"],
    yearIndex: 0,
    /**
     * sortBar
     */
    sortBar: {
      item1: {
        force: false,
        src_list: ["../../img/company/desBtn.png", "../../img/company/ascBtn.png"],
        bind_fun: "choose_year",
      },
      item2: {
        force: false,
        src_list: ["../../img/company/desBtn.png", "../../img/company/ascBtn.png"],
        bind_fun_1: "forecast_list_sort_by_time",
        bind_fun_2: "forecast_list_sort_by_time_des",
      },
      item3: {
        force: false,
        src_list: ["../../img/company/desBtn.png", "../../img/company/ascBtn.png"],
        bind_fun_1: "forecast_list_sort_by_principle",
        bind_fun_2: "forecast_list_sort_by_principle_des",
      },
      src_list_0: ["../../img/company/desBtn.png", "../../img/company/ascBtn.png"],
      src_list_1: ["../../img/company/desBtn_on.png", "../../img/company/ascBtn.png"],
      src_list_2: ["../../img/company/desBtn.png", "../../img/company/ascBtn_on.png"],
    },
    /**
       * tabBar
       */
    tabBar: null,
    /**
     * forecasrPanel
     */
    forecastList: [],

  },
  /**
   * 下拉列表 
   */
  bindYearChange: function (e) {
    console.log('picker year 发生选择改变，携带值为', e.detail.value);
    this.setData({
      yearIndex: e.detail.value
    })
  },

  /**
   * sortBar
   */
  forecast_list_sort_by_time: function () {
    var that = this;
    var list = that.data.forecastList;
    list.sort(that.sort_by_time);
    that.setData({
      forecastList: list,
      'sortBar.item2.src_list': that.data.sortBar.src_list_1,
      'sortBar.item2.force': true,
      'sortBar.item3.force': false,
      'sortBar.item3.src_list': that.data.sortBar.src_list_0,
    });


  },
  forecast_list_sort_by_time_des: function () {
    var that = this;
    var list = that.data.forecastList;
    list.sort(that.sort_by_time_des);
    that.setData({
      forecastList: list,
      'sortBar.item2.src_list': that.data.sortBar.src_list_2,
      'sortBar.item2.force': true,
      'sortBar.item3.force': false,
      'sortBar.item3.src_list': that.data.sortBar.src_list_0,
    });
  },
  forecast_list_sort_by_principle: function () {
    var that = this;
    var list = that.data.forecastList;
    list.sort(that.sort_by_principle);
    that.setData({
      forecastList: list,
      'sortBar.item3.src_list': that.data.sortBar.src_list_1,
      'sortBar.item3.force': true,
      'sortBar.item2.force': false,
      'sortBar.item2.src_list': that.data.sortBar.src_list_0,
    });
  },
  forecast_list_sort_by_principle_des: function () {
    var that = this;
    var list = that.data.forecastList;
    list.sort(that.sort_by_principle_des);
    that.setData({
      forecastList: list,
      'sortBar.item3.src_list': that.data.sortBar.src_list_2,
      'sortBar.item3.force': true,
      'sortBar.item2.force': false,
      'sortBar.item2.src_list': that.data.sortBar.src_list_0,
    });
  },
  sort_by_principle: function (a, b) {
    return a.principle - b.principle;
  },
  sort_by_principle_des: function (a, b) {
    console.log(b.principle - a.principle);
    return b.principle - a.principle;
  },
  sort_by_time: function (a, b) {
    // 获取某个时间格式的时间戳
    var timestampA = Date.parse(new Date(a.pay_time));
    timestampA = timestampA / 1000;
    var timestampB = Date.parse(new Date(b.pay_time));
    timestampB = timestampB / 1000

    return timestampA - timestampB;
  },
  sort_by_time_des: function (a, b) {
    // 获取某个时间格式的时间戳
    var timestampA = Date.parse(new Date(a.pay_time));
    timestampA = timestampA / 1000;
    var timestampB = Date.parse(new Date(b.pay_time));
    timestampB = timestampB / 1000

    return timestampB - timestampA;
  },
  /** 
   * forecastPanel
   */
  initStstusList: function () {
    var that = this;

    var panelLength = that.data.forecastList.length;

    for (var i = 0; i < panelLength; i++) {
      that.data.forecastList[i].status = false;
    }
  },
  initListYear: function () {
    var that = this;

    var panelLength = that.data.forecastList.length;
    for (var i = 0; i < panelLength; i++) {


      var pay_time = that.data.forecastList[i].pay_time;
      var pay_timeArr = pay_time.split("-");

      var item = "forecastList[" + i + "].year";
      var param = {};

      param[item] = pay_timeArr[0];
      that.setData(param);

    }
  },
  changeStatus: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    var status = that.data.forecastList[index].status;
    var item = "forecastList[" + index + "].status";

    var param = {};

    if (status) {
      param[item] = false;
      that.setData(param);

    } else {
      param[item] = true;
      that.setData(param);
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    // tabBar
    app.editTabBar();
    if (app.globalData.userType == 3) {

      //获取forecastList
      wx.request({
        url: app.globalData.server + '/predict/predict/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          console.log(res);

          if (res.data.data != null) {
            that.setData({
              forecastList: res.data.data,
            });
          }

          // 加入折叠标记
          that.initStstusList();
          that.initListYear();
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})